package com.example.jichu_1.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.jichu_1.R;
import com.example.jichu_1.bean.BanBean;
import com.example.jichu_1.bean.GirlBean;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter {
    Context context;

    public HomeAdapter(Context context) {
        this.context = context;
    }

    public List<BanBean.DataBean> banners=new ArrayList<>();

    public void addBanners(List<BanBean.DataBean> banners) {
        this.banners.addAll(banners);
        notifyDataSetChanged();
    }
    public List<GirlBean.DataBean> list=new ArrayList<>();

    public void addList(List<GirlBean.DataBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return 1;
        }
        return 2;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType==1){
            View view = LayoutInflater.from(context).inflate(R.layout.layout_banners, parent, false);
            return  new HomeBanner(view);
        }
        View view = LayoutInflater.from(context).inflate(R.layout.layout_item, parent, false);
        return new HomeHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType==1){
            HomeBanner homeBanner= (HomeBanner) holder;
            homeBanner.banners.setImages(banners);
            homeBanner.banners.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    BanBean.DataBean all= (BanBean.DataBean) path;
                    Glide.with(context).load(all.getImagePath()).into(imageView);
                }
            }).start();
        }
        if (itemViewType==2){
            HomeHolder homeHolder= (HomeHolder) holder;
            homeHolder.desc.setText(list.get(position-1).getDesc());
            Glide.with(context).load(list.get(position-1).getUrl()).into(homeHolder.iv);
        }
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (onMyItemClick!=null){
                    onMyItemClick.MyItemClick(position);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }
    class HomeBanner extends RecyclerView.ViewHolder {

        private final Banner banners;

        public HomeBanner(@NonNull View itemView) {
            super(itemView);
            banners = itemView.findViewById(R.id.banners);
        }
    }

    class HomeHolder extends RecyclerView.ViewHolder {

        private final TextView desc;
        private final ImageView iv;

        public HomeHolder(@NonNull View itemView) {
            super(itemView);
            desc = itemView.findViewById(R.id.item_desc);
            iv = itemView.findViewById(R.id.item_iv);
        }
    }
    public interface OnMyItemClick{
        void MyItemClick(int position);
    }
    OnMyItemClick onMyItemClick;

    public void setOnMyItemClick(OnMyItemClick onMyItemClick) {
        this.onMyItemClick = onMyItemClick;
    }
}
